package cn.icanci.snow.spring.util;

import cn.icanci.snow.spring.bean.BeanScanner;
import cn.icanci.snow.spring.bean.BeanScanners;

import java.util.*;

/**
 * Bean相关工具类
 *
 * @author icanci
 * @date Created in 2021/05/19 17:53
 * @version 1.0
 */
public class BeanUtil {

    /**
     * 所有被加载的包
     * @return 需要被加载的包的集合
     * @throws Exception 抛出异常
     */
    public static final List<String> loadBeanPackages() throws Exception {
        List<String> basePackageList = new ArrayList<>();
        // 去除重复定义的包
        HashSet<String> set = new HashSet<>();
        List<String> baseBeanScanner = loadBaseBeanScanner();
        List<String> baseBeanScanners = loadBaseBeanScanners();
        set.addAll(baseBeanScanner);
        set.addAll(baseBeanScanners);
        basePackageList.addAll(set);

        basePackageList.sort(((o1, o2) -> {
            return o1.compareTo(o1);
        }));
        set.clear();
        // 只有一个直接返回
        if (basePackageList.size() == 1) {
            return basePackageList;
        }
        // 合并父子包
        for (int i = basePackageList.size() - 1; i > 0; i--) {
            String first = basePackageList.get(i);
            String next = basePackageList.get(i - 1);
            if (first.startsWith(next) && set.contains(first)) {
                set.add(next);
                set.remove(first);
            } else {
                set.add(first);
                set.add(next);
            }
        }
        basePackageList.clear();
        basePackageList.addAll(set);
        return basePackageList;
    }

    /**
     * 获取需要加载bean的数组
     * @return 返回需要加载的包数组
     */
    private static final List<String> loadBaseBeanScanner() throws Exception {
        List<String> basePackageList = new ArrayList<>();
        ServiceLoader<BeanScanner> beanScannerServiceLoader = ServiceLoader.load(BeanScanner.class);
        Iterator<BeanScanner> iterator = beanScannerServiceLoader.iterator();
        while (iterator.hasNext()) {
            BeanScanner beanScanner = iterator.next();
            String packageName = beanScanner.loadBasePackage();
            if (ValidationUtil.isEmpty(packageName)) {
                continue;
            }
            basePackageList.add(packageName);
        }
        return basePackageList;
    }

    /**
     * 获取需要加载bean的数组
     * @return 返回需要加载的包数组
     */
    private static final List<String> loadBaseBeanScanners() throws Exception {
        List<String> basePackageList = new ArrayList<>();
        ServiceLoader<BeanScanners> beanScannersServiceLoader = ServiceLoader.load(BeanScanners.class);
        Iterator<BeanScanners> iterator = beanScannersServiceLoader.iterator();
        while (iterator.hasNext()) {
            BeanScanners beanScanners = iterator.next();
            List<String> packages = beanScanners.loadBasePackages();
            if (ValidationUtil.isEmpty(packages)) {
                continue;
            }
            basePackageList.addAll(packages);
        }
        return basePackageList;
    }

}
